﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;
using Class_Entity;
using System.Web.Configuration;

namespace DataAccess
{
    public class DATablet
    {
        SqlConnection con;
        SqlCommand cmd;
        


        public bool Add_Tablet(Tablet ta)
        {
            try
            {
                con = ConnectionToDataBase.StartConnection();
                con.Open();

                cmd = new SqlCommand("spAddTablet", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter data = new SqlDataAdapter(cmd);

                cmd.Parameters.AddWithValue("@Name", ta.Model);
                cmd.Parameters.AddWithValue("@Dies", ta.Dies);
                cmd.Parameters.AddWithValue("@MaxP", ta.MaxPressure);
                cmd.Parameters.AddWithValue("@MaxDia", ta.MaxDiameterOfTablet);
                cmd.Parameters.AddWithValue("@MaxDep", ta.MaxDepthOfFill);
                cmd.Parameters.AddWithValue("@Capa", ta.ProductionCapacity);
                cmd.Parameters.AddWithValue("@Size", ta.Size);
                cmd.Parameters.AddWithValue("@Weigth", ta.Weight);
                cmd.Parameters.AddWithValue("@Image", ta.Image);


                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ConnectionToDataBase.CloseConnection();
            }

            return true;
        }

        //--------------------------------------------------------------------------------------------

        public bool Edit_Tablet(Tablet ta)
        {
            try
            {
                con = ConnectionToDataBase.StartConnection();
                con.Open();

                cmd = new SqlCommand("spEditTablet", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter data = new SqlDataAdapter(cmd);

                cmd.Parameters.AddWithValue("@ID", ta.ID);
                cmd.Parameters.AddWithValue("@Name", ta.Model);
                cmd.Parameters.AddWithValue("@Dies", ta.Dies);
                cmd.Parameters.AddWithValue("@MaxP", ta.MaxPressure);
                cmd.Parameters.AddWithValue("@MaxDia", ta.MaxDiameterOfTablet);
                cmd.Parameters.AddWithValue("@MaxDep", ta.MaxDepthOfFill);
                cmd.Parameters.AddWithValue("@Capa", ta.ProductionCapacity);
                cmd.Parameters.AddWithValue("@Size", ta.Size);
                cmd.Parameters.AddWithValue("@Weigth", ta.Weight);
                cmd.Parameters.AddWithValue("@Image", ta.Image);


                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ConnectionToDataBase.CloseConnection();
            }

            return true;
        }


        //--------------------------------------------------------------------------------------------

        public bool Delete_Tablet(Tablet ta)
        {
            try
            {
                con = ConnectionToDataBase.StartConnection();
                con.Open();

                cmd = new SqlCommand("spDeleteTablet", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter data = new SqlDataAdapter(cmd);

                cmd.Parameters.AddWithValue("@ID", ta.ID);              
                
                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ConnectionToDataBase.CloseConnection();
            }

            return true;
        }


        //--------------------------------------------------------------------------------------------

        public DataSet Find_Tablet(Tablet ta)
        {
            DataSet ds = new DataSet();
            try
            {
                con = ConnectionToDataBase.StartConnection();
                con.Open();

                cmd = new SqlCommand("spFindTablet", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter data = new SqlDataAdapter(cmd);

                cmd.Parameters.AddWithValue("@ID", ta.ID);                


                cmd.ExecuteNonQuery();
                data.Fill(ds);
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ConnectionToDataBase.CloseConnection();
            }

            return ds;
        }


        //--------------------------------------------------------------------------------------------

        public DataSet Load_Tablet(Tablet ta)
        {
            DataSet ds = new DataSet();
            try
            {
                con = ConnectionToDataBase.StartConnection();
                con.Open();

                cmd = new SqlCommand("spLoadTablet", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter data = new SqlDataAdapter(cmd);
                
                cmd.ExecuteNonQuery();
                data.Fill(ds);
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ConnectionToDataBase.CloseConnection();
            }

            return ds;
        }

        //--------------------------------------------------------------------------------------------

        public DataSet Search_Tablet(Tablet ta)
        {
            DataSet ds = new DataSet();
            try
            {
                con = ConnectionToDataBase.StartConnection();
                con.Open();

                cmd = new SqlCommand("spSearchTablet", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter data = new SqlDataAdapter(cmd);

                cmd.Parameters.AddWithValue("@name", ta.Model);


                cmd.ExecuteNonQuery();
                data.Fill(ds);
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ConnectionToDataBase.CloseConnection();
            }

            return ds;
        }

        //--------------------------------------------------------------------------------------------

        public DataSet Detail_Tablet(Tablet ta)
        {
            DataSet ds = new DataSet();
            try
            {
                con = ConnectionToDataBase.StartConnection();
                con.Open();

                cmd = new SqlCommand("spDetailTablet", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter data = new SqlDataAdapter(cmd);

                cmd.Parameters.AddWithValue("@ID", ta.Model);


                cmd.ExecuteNonQuery();
                data.Fill(ds);
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ConnectionToDataBase.CloseConnection();
            }

            return ds;
        }


    }
}
